﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Hsinchu.BLL;
using Hsinchu.Utility;

public partial class Admin_ProductMgr_AddProperty : System.Web.UI.Page
{
    //获得配置文件路径
    string xmlDocPath = HttpContext.Current.Server.MapPath("~/") + "Config/PropertyTable.xml";
    protected void Page_Load(object sender, EventArgs e)
    {
        InitLoad();
    }

    void InitLoad()
    {
        BindPropertyGridView();
    }


    void BindPropertyGridView()
    {
        string tablename = HttpHandle.GetParamsString("TableName");
        ItemMgr dllItemMgr = new ItemMgr();
        InsertTableNode(tablename);
        this.FiledGridView.DataSource = dllItemMgr.GetProperty(xmlDocPath, "root//" + tablename);
        this.FiledGridView.DataBind();
    }   

    /// <summary>
    /// 添加新属性
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string tablename = HttpHandle.GetParamsString("TableName");
        InsertTableNode(tablename);
        string propertyename = this.txtPropertyEName.Text;
        string propertycname = this.txtPropertyCName.Text;
        string propertytype = this.drpPropertyType.SelectedValue;
        string propertysize = this.txtPropertySize.Text;
        string propertydisable = string.Empty;
        string propertydisplay = string.Empty;
        if (this.rbtnDisable.Checked)
        {
            propertydisable = this.rbtnDisable.Text;
        }
        if (this.rbtnUnDisable.Checked)
        {
            propertydisable = this.rbtnUnDisable.Text;
        }
        if (this.rbtnDisplay.Checked)
        {
            propertydisplay = this.rbtnDisplay.Text;
        }
        if (this.rbtnUnDisplay.Checked)
        {
            propertydisplay = this.rbtnUnDisplay.Text;
        }
        Dictionary<string, string> node = new Dictionary<string, string>();
        node.Add(propertyename, "");
        XMLHelper.InsertNode(xmlDocPath, "root//" + tablename, node);
        Dictionary<string, string> attribute = new Dictionary<string, string>();
        attribute.Add("PropertyEName", propertyename);
        attribute.Add("PropertyCName", propertycname);
        attribute.Add("PropertyType", propertytype);
        attribute.Add("PropertySize", propertysize);
        attribute.Add("PropertyDisable", propertydisable);
        attribute.Add("PropertyDisplay", propertydisplay);
        XMLHelper.InsertNodeAttribute(xmlDocPath, "root//" + tablename + "//" + propertyename, attribute);
        ItemMgr dllItemMgr = new ItemMgr();        
        dllItemMgr.AddProperty(tablename, propertyename, propertytype, propertysize);
        BindPropertyGridView();
    }

    /// <summary>
    /// 插入表节点
    /// </summary>
    /// <param name="TableName"></param>
    void InsertTableNode(string TableName)
    {
        //判断是否已经添加表节点
        if (!XMLHelper.IsExistNode(xmlDocPath, "root", TableName))
        {
            Dictionary<string, string> node = new Dictionary<string, string>();
            node.Add(TableName, "");
            XMLHelper.InsertNode(xmlDocPath, "root", node);
        }
    }




    /// <summary>
    /// 属性类型选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpPropertyType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.drpPropertyType.SelectedItem.Text != "字符型")
        {
            this.txtPropertySize.Enabled = false;
        }
        else
        {
            this.txtPropertySize.Enabled = true;
        }
    }



    /// <summary>
    /// GridView行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void FiledGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //修改序号
        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            e.Row.Cells[0].Text = (e.Row.RowIndex+1).ToString() ;
        }
        //修改默认值
        if (e.Row.Cells[3].Text != "varchar")
        {
            e.Row.Cells[4].Text = "默认大小";
        }
    }
}
